package org.codeart.juc.future;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class CompletableFutureDemo {

    public static void main(String[] args) throws IOException {
        // 如果不提供线程池，默认使用 ForkJoinPool，里面使用的是守护线程
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("异步任务开始执行");
            System.out.println("异步任务执行结束");
            return "OK";
        });

        CompletableFuture.runAsync(() -> {
            System.out.println("无返回值的任务开启");
            System.out.println("无返回值的任务结束");
        });

        CompletableFuture<String> taskA = CompletableFuture.supplyAsync(() -> {
            String id = UUID.randomUUID().toString();
            System.out.println("执行任务A: " + id);
            return id;
        });

        CompletableFuture<String> taskB = taskA.thenApply(r -> {
            System.out.println("任务B获取到任务A结果: " + r);
            r = r.replace("-", "");
            return r;
        });

        System.out.println(taskB.join());
    }

}
